package com.testdocs.client.structure.menu;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.StackLayoutPanel;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.Widget;
import com.testdocs.client.structure.menu.i18n.MenuConstants;
import com.testdocs.client.test.menu.TestGroupMenu;

public class MenuPanelImpl extends Composite implements MenuPanel{
	@UiTemplate("MenuPanel.ui.xml")
	interface MenuPanelUiBinder extends UiBinder<Widget, MenuPanelImpl> {
	}

	private static MenuPanelUiBinder uiBinder = GWT
			.create(MenuPanelUiBinder.class);
	
	private Presenter presenter;
	
	@UiField
	StackLayoutPanel stackPanel;
	
	@UiField
	Label testDocsStackLabel;
	@UiField
	Label plansStatsStackLabel;
	@UiField
	Label testingStackLabel;
	@UiField
	Label projectStackLabel;
	
	@UiField
	ScrollPanel testDocsPanel;
	
	@UiField
	SimplePanel projectSection;

	Tree projectMenu;
	TreeItem currentItem;
	
	TestGroupMenu testGroupMenu;
	
	private Map<MenuItem, Integer> stackItemsMap = new HashMap<MenuItem, Integer>();
	private Map<MenuItem, TreeItem> treeItemsMap = new HashMap<MenuItem, TreeItem>();
	
	private MenuConstants menuConstants = (MenuConstants) GWT.create(MenuConstants.class);
	
	public MenuPanelImpl() {
		initWidget(uiBinder.createAndBindUi(this));
		buildStackPanel();
		buildProjectMenuItems();
		buildTestDocsMenu();
	}
	
	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}
	
	private void buildStackPanel() {
		testDocsStackLabel.setText(menuConstants.testDocsStack());
		plansStatsStackLabel.setText(menuConstants.plansStatsStack());
		testingStackLabel.setText(menuConstants.testingStack());
		projectStackLabel.setText(menuConstants.projectStack());
	}
	
	private void buildProjectMenuItems() {
		stackItemsMap.put(MenuItem.TEST_GROUP, 0);
		
		// Dashboard menu item
		Anchor dashboardAnchor = new Anchor(menuConstants.projectMenuItemDashboard());
		TreeItem dashboardItem = new TreeItem(dashboardAnchor);
		dashboardAnchor.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				presenter.onSelectDashboard();
			}
		});
		stackItemsMap.put(MenuItem.DASHBOARD, 3);
		treeItemsMap.put(MenuItem.DASHBOARD, dashboardItem);

		//Versions menu item
		Anchor versionsAnchor = new Anchor(menuConstants.projectMenuItemVersions());
		TreeItem versionsItem = new TreeItem(versionsAnchor);
		versionsAnchor.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				presenter.onSelectVersionList();
			}
		});
		stackItemsMap.put(MenuItem.VERSIONS, 3);
		treeItemsMap.put(MenuItem.VERSIONS, versionsItem);

		projectMenu = new Tree();
		projectMenu.addItem(treeItemsMap.get(MenuItem.DASHBOARD));			
		projectMenu.addItem(treeItemsMap.get(MenuItem.VERSIONS));			

		projectSection.setWidget(projectMenu);

	}
	
	private void buildTestDocsMenu() {
		stackPanel.addSelectionHandler(new SelectionHandler<Integer>() {
			@Override
			public void onSelection(SelectionEvent<Integer> event) {
				if (event.getSelectedItem() == 0) {
					presenter.onOpenTestDocs();
				}
			}
		});
	}
	
	@Override
	public void selectMenuItem(MenuItem menuItem) {
		if (stackItemsMap.containsKey(menuItem)) {
			stackPanel.showWidget(stackItemsMap.get(menuItem), false);			
		}

		if (currentItem != null) {
			currentItem.setSelected(false);			
		}

		TreeItem item = null;
		if (treeItemsMap.containsKey(menuItem)) {
			item = treeItemsMap.get(menuItem);
			item.setSelected(true);
			
			testGroupMenu.deselect();
		}
		
		currentItem = item;
	}
	
	@Override
	public void setTestGroupMenu(TestGroupMenu testGroupMenu) {
		testDocsPanel.setWidget(testGroupMenu);
		this.testGroupMenu = testGroupMenu;
	}
}
